package br.com.klis.batendoumabola.client.presenter;

import java.util.Date;

import br.com.klis.batendoumabola.client.event.EditPeladaCancelledEvent;
import br.com.klis.batendoumabola.client.event.PeladaUpdatedEvent;
import br.com.klis.batendoumabola.client.service.PeladaServiceAsync;
import br.com.klis.batendoumabola.shared.Pelada;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;

public class ContentPresenter implements Presenter{  
  public interface Display {
    HasClickHandlers getSearchButton();
    HasClickHandlers getSaveButton();
    HasClickHandlers getCancelButton();
    
    HasValue<String> getQual();
    HasValue<String> getOnde();
    HasValue<Date> getQuando();
    
    HasValue<String> getNome();
    HasValue<String> getDescricao();
    HasValue<Date> getData();
    
    void setFormAsEdicao();
    void setFormAsPesquisa();
    
    Widget asWidget();
  }
  
  private Pelada pelada;
  private final PeladaServiceAsync rpcService;
  private final HandlerManager eventBus;
  private final Display display;
  
  public ContentPresenter(PeladaServiceAsync rpcService, HandlerManager eventBus, Display display) {
    this.rpcService = rpcService;
    this.eventBus = eventBus;
    this.pelada = new Pelada();
    this.display = display;
    ContentPresenter.this.display.setFormAsPesquisa();
    bind();
  }
  
  public ContentPresenter(PeladaServiceAsync rpcService, HandlerManager eventBus, Display display, String tipo) {
    this.rpcService = rpcService;
    this.eventBus = eventBus;
    this.display = display;
    if (tipo.equals("PESQUISA")){
    	ContentPresenter.this.display.setFormAsPesquisa();
    }else{
    	ContentPresenter.this.display.setFormAsEdicao();
    }
    bind();
    
   /* rpcService.getPelada(id, new AsyncCallback<Pelada>() {
      public void onSuccess(Pelada result) {
        pelada = result;
        ContentPresenter.this.display.getNome().setValue(pelada.getNome());
        ContentPresenter.this.display.getDescricao().setValue(pelada.getDescricao());
        ContentPresenter.this.display.getData().setValue(pelada.getData());
      }
      
      public void onFailure(Throwable caught) {
        Window.alert("Error retrieving pelada");
      }
    });*/
    
  }
  
  public void bind() {
    this.display.getSaveButton().addClickHandler(new ClickHandler() {   
      public void onClick(ClickEvent event) {
        doSave();
      }
    });

    this.display.getCancelButton().addClickHandler(new ClickHandler() {   
      public void onClick(ClickEvent event) {
        eventBus.fireEvent(new EditPeladaCancelledEvent());
      }
    });
  }

  public void go(final HasWidgets container) {
    container.clear();
    container.add(display.asWidget());
  }

  private void doSave() {
    pelada.setNome(display.getNome().getValue());
    pelada.setDescricao(display.getDescricao().getValue());
    pelada.setData(display.getData().getValue());
    
    rpcService.savePelada(pelada, new AsyncCallback<Pelada>() {
        public void onSuccess(Pelada result) {
          eventBus.fireEvent(new PeladaUpdatedEvent(result));
        }
        public void onFailure(Throwable caught) {
          Window.alert("Error updating pelada");
        }
    });
  }
  
}
